package com.example.scorecard;

import javax.servlet.annotation.WebServlet;

import com.home.facade.AccountFacade;
import com.home.model.dto.AccountDto;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
@Theme("scorecard")
public class ScorecardUI extends UI {

	@WebServlet(value = "/*", asyncSupported = true)
	@VaadinServletConfiguration(productionMode = false, ui = ScorecardUI.class)
	public static class Servlet extends VaadinServlet {
	}

	@Override
	protected void init(VaadinRequest request) {
		final VerticalLayout layout = new VerticalLayout();
		layout.setMargin(true);
		setContent(layout);
		
		final TextField txtUserName = new TextField();
		layout.addComponent(txtUserName);
		final PasswordField txtPassword = new PasswordField();
		layout.addComponent(txtPassword);
		Button button = new Button("Click Me");
		button.addClickListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				AccountFacade accountFacade = new AccountFacade();

				AccountDto acc = accountFacade.isValidLogin(txtUserName.getValue(), txtPassword.getValue());
				if (acc != null) {
					layout.addComponent(new Label("Login successfully"));					
				} else {
					layout.addComponent(new Label("Login failed"));
				}
			}
		});
		layout.addComponent(button);
	}

}